package com.mybatis.panel;

import com.mybatis.entity.AwardPunish;
import com.mybatis.entity.Student;
import com.mybatis.service.AwardPunishService;
import com.mybatis.service.StudentService;
import com.mybatis.util.DataUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class AwardPunishInsertPanel extends JPanel {

    public AwardPunishInsertPanel() {
        setBounds(0, 0, 900, 600);

        JPanel panel2 = new JPanel();
        add(panel2, BorderLayout.NORTH);

        JButton button = new JButton("返回主页");
        add(button);

        JButton insertButton = new JButton("向表插入行中数据");
        add(insertButton);


        //表格
        JTable table = new JTable() {
            @Override
            public boolean isCellEditable(int row, int column) {
                //可以直接编辑表格
                return true;
            }
        };

        Object[] columnNames = {"奖惩记录号", "学号", "奖惩记录详情", "", "", "", "", "", "", "", ""};

        //List<Student> list = new ArrayList<>();
        //list.add(new Student());
        //List<Student> list = StudentService.getPage((pageSize - 1) * 20);

        Student student = StudentService.getBySno(StudentPanel.dataRow);

        Integer apno = AwardPunishService.getapno();
        apno++;
        System.out.println("=======================");
        System.out.println(apno);
        AwardPunish awardPunish = new AwardPunish();
        awardPunish.setSno(student.getSno());
        awardPunish.setApno(apno.toString());
        List<AwardPunish> list = new ArrayList<>();
        list.add(awardPunish);

        Object[][] objects = DataUtil.getConvertData(list);

        DefaultTableModel tableModel = new DefaultTableModel(objects, columnNames);

        //设置表格数据只能单选
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //设置表格数据
        table.setModel(tableModel);

        //panel.add(table);
        //panel.add(table.getTableHeader(), BorderLayout.NORTH);
        add(table.getTableHeader(), BorderLayout.NORTH);
        //把表格内容添加到容器中心
        //panel.add(table, BorderLayout.CENTER);
        add(table, BorderLayout.CENTER);


        //================按钮事件===================

        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                MainJFrame.setPanel(new OtherPanel(0));
            }
        });

        insertButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //Student student = new Student();
                AwardPunish awardPunish = new AwardPunish();
                Object[] objects = new Object[9];
                for (int i = 0; i < 3; i++) {
                    objects[i] = table.getValueAt(0, i);
                }
                awardPunish.setApno((String) objects[0]);
                awardPunish.setSno((String) objects[1]);
                awardPunish.setAprecord((String) objects[2]);

                AwardPunishService.insert(awardPunish);
                JOptionPane.showMessageDialog(panel2,
                        "插入成功!", "系统提示", JOptionPane.WARNING_MESSAGE);
                MainJFrame.setPanel(new OtherPanel(0));

            }
        });
    }
}
